<?php
/*
Copyright (c) 2006 Darrell Schulte, 2008 Alex Tingle, 2011 Lorenzo De Tomasi.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
$default_no_events_text = __('No events','wpevents');
class wpevents_list_widget extends WP_Widget {
  function wpevents_list_widget() {
    // use parent constructor to re-write standard class properties
    parent::WP_Widget('ec3-event-list', __('Upcoming Events','ec3'), array('description' => __('Display upcoming events as a list'.' ('.__('WP-Events plugin').').','ec3'), 'class' => 'wpevents_list_widget'));
  }
  
  function widget($args, $instance) {
    global $default_no_events_text;
    // Outputs the content of the widget.
    // Subclasses should over-ride this function to generate their widget code.
    extract($args,EXTR_SKIP);
    echo $before_widget . $before_title;
    echo ec3_widget_title($instance['title'],'Upcoming Events');
    echo $after_title;
    if(ec3_check_installed(__('Upcoming Events','ec3'))) {
      // Parse $limit:
      //  NUMBER      - limits number of posts
      //  NUMBER days - next NUMBER of days
      $cat = $instance['cat'];
      $no_events_text = $instance['no_events_text'];
      $show_times = $instance['show_times'];
      $show_locations = $instance['show_locations'];
      $limit = $instance['limit'];
      $num =intval($limit);
      if(strpos($limit, '&') !== false) {
        $has_vars = 'multiple';
      } elseif(!$has_vars && strpos($limit, '=') !== false) {
        $has_vars = 'single';
      }
      $query = new WP_Query();
      if(preg_match('/^ *([0-9]+) *d(ays?)?/',$limit,$matches)) {
        $query_string = 'ec3_days='.intval($matches[1]);
      } elseif($num>0) {
        $query_string = 'ec3_after=today&posts_per_page='.$num;
      } elseif($num<0) {
        $query_string = 'ec3_before=today&order=asc&posts_per_page='.abs($num);
      } elseif($has_vars == 'single' || $has_vars == 'multiple') {
        $query_string = $limit;
      } else {
        $query_string = 'ec3_after=today&posts_per_page=5';
      }
      if($cat) { $query_string .= '&cat='.$cat; }
      $query->query( $query_string );
?>
  <ul class="events-list">
<!-- Generated by WP-Events v. <?php echo ec3_get_version(); ?> -->
<?php
      if($query->have_posts()) {
        $current_date=false;
        for($evt=ec3_iter_all_events_q($query); $evt->valid(); $evt->next()) {
          // Date changed?
          $date=ec3_get_start_datetime('j F Y');
          if(!$current_date || $current_date!=$date) {
            if($current_date) {
?>
  </ul>
</li>
<?php
            }
?>
<li class="date">
  <h3 class="startdate"><?php echo $date; ?></h3>
  <ul>
<?php
            $current_date=$date;
          }
          // Print the event.
?>
    <li class="event" itemscope itemtype="http://schema.org/Event">
      <a itemprop="url" href="<?php echo get_permalink(); ?>"><span itemprop="name"><?php echo get_the_title(); ?></span> (<time class="startdate" itemprop="startDate" datetime="<?php echo ec3_get_start_datetime('c'); ?>"><?php echo ec3_get_start_datetime('G:i'); ?></time>)</a>
      <time class="enddate" itemprop="endDate" datetime="<?php echo ec3_get_end_datetime('c'); ?>"></time>
<?php
          //Display location infos.
          if($show_locations == on && ec3_get_location()) {
?>
            , <span class="location" itemprop="location" itemtype="http://schema.org/Place"><?php echo __(ec3_get_location()); ?></span>
<?php
          }

?>
    </li>
<?php
        }
        if($current_date) {
?>
  </ul>
</li>
<?php
        }
      } else {
?>
  <li><?php echo $default_no_events_text; ?></li>
<?php
      }
?>
</ul>
<?php
    }
    echo $after_widget;    
  }

  function form($instance) {
    global $default_no_events_text;
    // outputs the options form on admin
    $limit = $instance['limit'];
    if(empty($limit)) {
      $limit = 5;
    } else {
      $limit = htmlspecialchars($limit,ENT_QUOTES);
    }
    $cat = $instance['cat'];
    $cat = htmlspecialchars($cat,ENT_QUOTES);
    $no_events_text = $instance['no_events_text'];
    if(empty($no_events_text)) {
      $no_events_text = $default_no_events_text;
    } else {
      $no_events_text = $no_events_text;
    }
    echo $default_no_events_text;
    $show_times = $instance['show_times'];
    $show_locations = $instance['show_locations'];
?>
    <p>
     <label for="<?php echo $this->get_field_id('title') ?>">
      <?php _e('Title:'); ?>
      <input class="widefat" id="<?php echo $this->get_field_id('title') ?>"
       name="<?php echo $this->get_field_name('title') ?>" type="text"
       value="<?php echo htmlspecialchars($instance['title'],ENT_QUOTES) ?>" />
     </label>
    </p>

    <p>
     <label for="<?php echo $this->get_field_id('limit') ?>"
      title="<?php echo $limit_title ?>">
      <?php _e('Display events','ec3') ?>:
      <input class="widefat" style="width: 50px; text-align: center;"
       id="<?php echo $this->get_field_id('limit') ?>"
       name="<?php echo $this->get_field_name('limit') ?>" type="text"
       value="<?php echo $limit ?>" /><br />
       <small><?php echo __('Examples: <code>5</code>, <code>5 days</code>, <code>5d</code>. To display recent past events, use a negative number: <code>-5</code>.').' <a href="'.__('http://wpcal.firetree.net/advanced-queries/').'" title="'.__('View documentation').'">'.__('Advanced options').'</a>'; ?></small>
     </label>
    </p>

    <p>
     <label for="<?php echo $this->get_field_id('cat') ?>">
      <?php _e('Show events in categories','ec3') ?>:
      <input class="widefat" style="width: 50px; text-align: center;"
       id="<?php echo $this->get_field_id('cat') ?>"
       name="<?php echo $this->get_field_name('cat') ?>" type="text"
       value="<?php echo $cat ?>" /><br />
       <small><?php _e('Examples: <code>5</code>, <code>5,10</code>. To exclude a category, use a negative number: <code>-10</code>.'); ?></small>
     </label>
    </p>
    
    <p>
     <label for="<?php echo $this->get_field_id('no_events_text') ?>">
      <?php printf( __('Custom <q>%1$s</q> text'), $default_no_events_text); ?>:
      <input class="widefat"
       id="<?php echo $this->get_field_id('no_events_text') ?>"
       name="<?php echo $this->get_field_name('no_events_text') ?>" type="text"
       value="<?php _e($no_events_text); ?>" />
     </label>
    </p>
    
    <p>
     <label for="<?php echo $this->get_field_id('show_times') ?>">
      <?php printf( __('Show <q>%1$s</q> text'), __('All day','ec3')); ?>:
      <input type="checkbox"
        id="<?php echo $this->get_field_id('show_times') ?>"
        name="<?php echo $this->get_field_name('show_times') ?>" type="text"
        <?php if($show_times == on) { echo 'checked="checked"'; } ?>
      />
     </label>
    </p>
    
    <p>
     <label for="<?php echo $this->get_field_id('show_locations') ?>">
      <?php _e( 'Show locations'); ?>:
      <input type="checkbox"
        id="<?php echo $this->get_field_id('show_locations') ?>"
        name="<?php echo $this->get_field_name('show_locations') ?>" type="text"
        <?php if($show_locations == on) { echo 'checked="checked"'; } ?>
      />
     </label>
    </p>
    <p><a href="options-general.php?page=ec3_admin"><?php _e('Go to Event Calendar Options','ec3') ?>.</a></p>

    <input type="hidden" id="<?php echo $this->get_field_id('submit') ?>" name="<?php echo $this->get_field_name('submit') ?>" value="1" />
<?php
  }

  function update($new_instance, $old_instance) {
    // Processes widget options to be saved
    // This function should check that $new_instance is set correctly.
    // The newly calculated value of $instance should be returned.
    if( !isset($new_instance['title']) ) // user clicked cancel
        return false;
    // fill current state with old data to be sure we not loose anything
    $instance = $old_instance;
    $instance['title'] = strip_tags(stripslashes($new_instance['title']));
    $instance['limit'] = strip_tags(stripslashes($new_instance['limit']));
    $instance['cat'] = strip_tags(stripslashes($new_instance['cat']));
    $instance['no_events_text'] = strip_tags(stripslashes($new_instance['no_events_text']));
    $instance['show_times'] = strip_tags(stripslashes($new_instance['show_times']));
    $instance['show_locations'] = strip_tags(stripslashes($new_instance['show_locations']));
    return $instance;
  }
}
function register_wpevents_list_widget() {
  register_widget('wpevents_list_widget');
}
add_action('init', 'register_wpevents_list_widget', 1);